package com.example.administrator.rxjavadiydemo.model;

import com.example.administrator.rxjavadiydemo.api.ServerApi;
import com.example.administrator.rxjavadiydemo.bean.HomeBean;
import com.example.administrator.rxjavadiydemo.interfaces.IMain_getData;
import com.example.administrator.rxjavadiydemo.presenter.Presenter_Main;
import com.example.administrator.rxjavadiydemo.util.RxJavaNetUtils;
import com.example.library.utils.L;
import com.example.library.utils.netutils.IDataCallBack;
import com.example.library.utils.netutils.Retrofit2Helper;

import retrofit2.Retrofit;
import rx.Observable;
import rx.Subscription;


/**
 * Created by lw on 2017/11/21.
 */

public class Model_Main {

    public static final String MAIN_URL ="http://news-at.zhihu.com/api/4/";
    private Subscription mSubscription;
    public void getData(final IMain_getData Imain){
    Retrofit retrofit = Retrofit2Helper.getInstance().getRetrofit(MAIN_URL);
    ServerApi serverApi = retrofit.create(ServerApi.class);
    Observable<HomeBean> columnData = serverApi.getColumnData();
    mSubscription = RxJavaNetUtils.getInstance().deploy(columnData, new IDataCallBack<HomeBean>() {
        @Override
        public void onCompleted(HomeBean homeBean) {
            Imain.completed(homeBean);
        }

        @Override
        public void onFailed(String errMsg) {
            Imain.onError(errMsg);
        }

        @Override
        public void before() {
            Imain.before();

        }

        @Override
        public void after() {
            Imain.after();
        }
    });

    }

    public void onDestroy() {
        if(mSubscription!=null&&mSubscription.isUnsubscribed())
            mSubscription.unsubscribe();
    }


}
